package com.mjf.srm.util;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mjf.common.utils.sign.Md5Utils;
import com.mjf.srm.config.SRMConfig;
import com.mjf.srm.enums.SRMApiEnum;
import com.mjf.srm.vo.PURInfoVO;
import com.mjf.srm.vo.PURListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;

/**
 * <p>
 * desc
 * </p>
 *
 * @author chenshoumeng
 * @date 2025/6/29 00:23
 */
public class SRMSendUtil {

    public static void main(String[] args) {
        String sign = getSupplierLoginCode("https://apigw.cccar.com.cn/openapi/test/js/a/coop/vendor/verify",
            "70003276", "CRC");
//        String sign = SRMSendUtil.getSupplierLoginCode(srmConfig.getUrl(), srmConfig.getSupplierCode(),
//            srmConfig.getEncryptPrefix());
//        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("verify_code", sign);
//        JSONObject result = SRMSendUtil.sendPost("https://apigw.cccar.com.cn" + SRMApiEnum.PUR_LIST.getUrl(), jsonObject);
//        ObjectMapper objectMapper = new ObjectMapper();
//        objectMapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
//        try {
//            assert result != null;
//            PURListVO vo = objectMapper.readValue(result.toString(), PURListVO.class);
//            System.out.println(vo);
//        } catch (JsonProcessingException e) {
//            e.printStackTrace();
//
//        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("verify_code", sign);
        jsonObject.put("pur_header_id", "396689252");
        JSONObject result = SRMSendUtil.sendPost("https://apigw.cccar.com.cn" + SRMApiEnum.PUR_FIND.getUrl(), jsonObject);
        System.out.println( result.toString());
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
        try {
            assert result != null;
            PURInfoVO vo = objectMapper.readValue(result.toString(), PURInfoVO.class);
            System.out.println(vo);
        } catch (Exception e) {
            e.printStackTrace();

        }

    }

    /**
     * 获取供应商登录验证码
     */
    public static String getSupplierLoginCode(String url, String supplierCode, String encryptPrefix) {
        // yyyyMMdd
        String date = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
        String sign = DigestUtil.md5Hex(DigestUtil.md5Hex(encryptPrefix+supplierCode + date).toUpperCase()).toUpperCase();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("verifyStr", sign);
        JSONObject result = sendPost(url, jsonObject);
        if (sendSuccess(result)) {
            return result.getStr("msg");
        }
        return"";
    }

    /**
     * 判断请求结果
     * @param result
     * @return
     */
    public static Boolean sendSuccess(JSONObject result) {
        return ObjectUtil.isNotNull(result) && "S".equals(result.getStr("status"));
    }

    /**
     * 发送请求
     * @param url
     * @param bodyJson
     * @return
     */
    public static JSONObject sendPost(String url, JSONObject bodyJson) {
        System.out.println("请求参数：" + bodyJson.toString());
        try {
            HttpResponse response = HttpRequest.post(url)
                .header("Content-Type", "application/json")
                .body(bodyJson.toString())
                .timeout(10000) // 超时时间可设置
                .execute();
            return JSONUtil.parseObj(response.body());
        } catch (Exception e) {
            e.printStackTrace();
        }
       return null;
    }


}
